/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import conexion.Conexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.InterfazFacadeModelo;

/**
 *
 * @author carlos
 */
public abstract class AbstractFacade 
{
    protected abstract Object getObjeto(ResultSet r);
    protected abstract String getTabla();
    protected abstract String[] estructuraTabla();
    protected Conexion conexion;

    public AbstractFacade() 
    {
        conexion=Conexion.getConexion();
    }
    
    
          
    public void grabar(InterfazFacadeModelo interfaz)
    {
       String consulta="INSERT INTO "+getTabla()+"(";
       String[] vector=interfaz.getDatos();
       String[] estructura=estructuraTabla();
       
    
       for (int index = 0; index < estructura.length; index++) 
       {
           if(index==estructura.length-1)
               consulta=consulta+estructura[index];    
           else     
                consulta=consulta+estructura[index]+",";
       }
       consulta=consulta+") VALUES (";
       
       
       for (int index = 0; index < vector.length; index++) 
       {
           if(index==0)
           {
               if(vector[index].equals(""))
               {
                   consulta=consulta+"'0',";    
               }
               else
               {
                   consulta=consulta+"'"+vector[index]+"',";    
               }
           }
           else
           {
           
                if(index==vector.length-1)
                    consulta=consulta+"'"+vector[index]+"'";    
                else     
                    consulta=consulta+"'"+vector[index]+"',";
           }
       }
       
       consulta=consulta+");";
       System.err.print(consulta);
       conexion.update(consulta);
       
    }
    public void editar(InterfazFacadeModelo interfaz)
    {
        String consulta = "UPDATE " + this.getTabla() + " SET ";
        String []vector = interfaz.getDatos();
        String[] estructura=estructuraTabla();
        
        for (int index = 0; index < vector.length; index++) {
            if (index == vector.length - 1)
                consulta = consulta +estructura[index] + "='" + vector[index] + "'";
            else
                consulta = consulta + estructura[index] + "='" + vector[index]+ "'" + ",";
        }
        consulta = consulta + " WHERE " + estructura[0] + "='" + vector[0]+ "';";
        // echo $consulta;            
        //$Result1 = $this->conexion->ejecutarConsulta($consulta, $this->conexion) or die(mysql_error());
        System.err.println(consulta);
        conexion.update(consulta);
    }
    
    public void eliminar(InterfazFacadeModelo interfaz)
    {
        String datos[]=interfaz.getDatos();
        String consulta="DELETE FROM  "+getTabla()+" where "+estructuraTabla()[0]+"='"+datos[0]+"'";
        System.err.println(consulta);
        conexion.update(consulta);
    }
    
    public void eliminar(String codigo)
    {
        
        String consulta="DELETE FROM  "+getTabla()+" where "+estructuraTabla()[0]+"='"+codigo+"'";
        System.err.println(consulta);
        conexion.update(consulta);
    }
    
    public Vector<Object> getAll()
    {
        try {
            Vector<Object> lista=new Vector<>();
            String consulta="SELECT * FROM "+this.getTabla();
            ResultSet r=conexion.select(consulta);
            
            if(r!=null)
            {
                while(r.next())
                {
                    lista.add(getObjeto(r));
                }
            }
            else
            {
                return null;
            }
            
            
            return lista;
        } catch (SQLException ex) {
            Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;        
    }
    
    public Object getFind(String codigo)
    {
        String consulta="SELECT * FROM "+this.getTabla()+" WHERE "+estructuraTabla()[0]+" = '"+codigo+"'";
        ResultSet r=conexion.select(consulta);
        try {
            r.next();
        } catch (SQLException ex) {
            Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(r!=null)
            return getObjeto(r);
        else
            return null;
    }
    
    public String[] getEstructuraTabla()
    {
        return this.estructuraTabla();
    }
}
